package com.zhihuishu.micro.course.openapi.course.impl;

import com.zhihuishu.micro.course.helper.RedisKeyHelper;
import com.zhihuishu.micro.course.openapi.course.OverSeaCourseCleanRedis;
import com.zhihuishu.remote.RemoteException;
import com.zhihuishu.remote.RemoteResult;
import com.zhihuishu.toolkit.jedis.JedisHelper;
import com.zhihuishu.toolkit.jedis.JedisHelper.Handler;
import com.zhihuishu.toolkit.jedis.template.JedisExecutor;
import org.springframework.util.CollectionUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;

import javax.annotation.Resource;
import java.util.List;

public class OverSeaCourseCleanRedisImpl implements OverSeaCourseCleanRedis {

	
	 @Resource
	    private JedisExecutor jedisExecutor;
		
	@Override
	public RemoteResult<Void> delProgressByCourseIds(final List<Long> courseIds) throws RemoteException {
		
        RemoteResult<Void> r = new RemoteResult<Void>();
        if (!CollectionUtils.isEmpty(courseIds)) {
           /* Jedis jedis = null;
            try {
                jedis = jedisPool.getResource();
                Pipeline pip = jedis.pipelined();
                String progressInfoKey = "";
                for (Long courseId : courseIds) {
                    progressInfoKey = RedisKeyHelper.getProgressInfoKey(courseId);
                    pip.del(progressInfoKey);
                }
            } finally {
                JedisHelper.closeQuietly(jedis);
            }*/
            jedisExecutor.execute(new Handler() {
                
                @Override
                public void handle(Jedis jedis) {
                    Pipeline pip = jedis.pipelined();
                    String progressInfoKey = "";
                    for (Long courseId : courseIds) {
                        progressInfoKey = RedisKeyHelper.getProgressInfoKey(courseId);
                        pip.del(progressInfoKey);
                    }
                    
                }
            });
        }
        return r;
	}


	@Override
	public RemoteResult<Void> delCourseSpeakerById(final Long courseId) throws RemoteException {
		RemoteResult<Void> result = new RemoteResult<Void>();
		
		jedisExecutor.execute( new Handler() {

			@Override
			public void handle(Jedis jedis) {
				jedis.del(RedisKeyHelper.getCourseSpeakerIdsKey(courseId));
			}
		});
		return result;
	}


	@Override
	public RemoteResult<Void> deCourseClipsById(final Long clipsId) throws RemoteException {
		
		RemoteResult<Void> result = new RemoteResult<Void>();
		jedisExecutor.execute( new Handler() {
		@Override
		public void handle(Jedis jedis) {
			jedis.del(JedisHelper.key("cc:course:clips" ,clipsId)) ;
		}
	  });
		return result;
	}

}
